package com.diuse.outer.analyse.tongcheng;

import com.diuse.outer.analyse.util.AmapHelper;
import com.diuse.outer.common.Constant;
import com.diuse.outer.executor.HttpHelper;
import org.apache.log4j.Logger;
import org.htmlparser.util.ParserException;

import java.io.IOException;

/**
 * Created by Administrator on 2015/11/24 0024.
 */
public class DetailHtmlParser {
    private String url;
    private String location;
    private String agentName;
    private String agentPhone;
    private static Logger logger = Logger.getLogger(DetailHtmlParser.class);

    public DetailHtmlParser(String url) {
        this.url = url;
    }

    private static void message( String szMsg ) {
        try{
            System.out.println(new String(szMsg.getBytes(Constant.ENCODE), System.getProperty("file.encoding")));
        } catch(Exception e ){

        }
    }

    public static void main(String[] args) {
        String urlTest = "http://hz.58.com/zufang/24084026572339x.shtml?version=&psid=120093194189837796080930408&entinfo=24084026572339_0";
        DetailHtmlParser test = new DetailHtmlParser(urlTest);
        try {
            test.analyse();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserException e) {
            e.printStackTrace();
        }
    }

    public void analyse() throws IOException, ParserException {

        String result = HttpHelper.requestGet(url);
        String locStartStr = "<div class=\"su_tit\">地址</div>";
        int locStartIndex = result.indexOf(locStartStr);
        if (locStartIndex == -1 ) {
            logger.error(Constant.Log.PARSER_LOCATION_ERROR + url);
            return;
        }
        locStartIndex += locStartStr.length();
        locStartIndex = result.indexOf("\">", locStartIndex);
        locStartIndex +=  "\">".length();
        int locEndIndex = result.indexOf("<span", locStartIndex);
        location = result.substring(locStartIndex, locEndIndex).trim();

        System.out.println("location  " + location);
        AmapHelper.LngLat lngLat = new AmapHelper(location).get();
        System.out.println("lng: " + lngLat.getLng() + "lat: " + lngLat.getLat());
        //依据了网页的上下顺序，商用位置的结束位置作为当前检索的起始点，以增加效率
        String nameStartStr = "<div class=\"su_tit\" item=\"6\" testi=\"11\">联系</div>";
        int agentStartIndex = result.indexOf(nameStartStr, locEndIndex);
        if (agentStartIndex == -1) {
            logger.error(Constant.Log.PARSER_AGENT_ERROR + url);
            return;
        }
        agentStartIndex += nameStartStr.length();
        agentStartIndex = result.indexOf("<a", agentStartIndex);
        if (agentStartIndex == -1) {
            logger.error(Constant.Log.PARSER_AGENT_ERROR + url);
            return;
        }
        agentStartIndex += "<a rel=\"".length();
        agentStartIndex = result.indexOf("\">", agentStartIndex);
        agentStartIndex += "\">".length();
        int agentEndIndex = result.indexOf("</a>", agentStartIndex);
        agentName = result.substring(agentStartIndex, agentEndIndex).trim();
        System.out.println("agent name  " + agentName);

        String phoneStartStr = "<span id=\"t_phone\" class=\"arial c_e22\" style=\"font-size:26px;font-weight:bold;\">";
        int phoneStartIndex = result.indexOf(phoneStartStr, agentEndIndex);
        if (phoneStartIndex == -1) {
            logger.error(Constant.Log.PARSER_PHONE_ERROR + url);
            return;
        }
        phoneStartIndex += phoneStartStr.length();
        int phoneEndIndex = result.indexOf("</span>", phoneStartIndex);
        agentPhone = result.substring(phoneStartIndex, phoneEndIndex).trim();
        System.out.println("agent phone  " + agentPhone);
    }
}
